﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
namespace homework9
{
    public partial class Form1 : Form
    {  
        //@符号前有若干英文字符
        //@符号只有一个
        //@和 . 之前后可以跟数字或英文，如qq.com,126.com
        //.只有一个， . 后未2-4个英文字母
        private static string emailRE = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        //一般电话号码需要有-，-前为三个数字，-后为八个数字
        private static string telphoneRE = @"(\d{3})+([-]{1})+(\d{8})";
        //手机号为11位而且是1开头
        private static string phoneRE = @"1{1}\d{10}";




        public Form1()
        {
            InitializeComponent();
        }
        //WebClient类提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。
        //DownloadString  以 String 形式下载请求的资源。
        public string getHtmlString(string url)
        {
            try
            {
                WebClient client = new WebClient();
                client.Encoding = Encoding.UTF8;
                string reply = client.DownloadString(url);
                return reply;
            }
            catch
            {
                MessageBox.Show("发生错误");
                return null;
            }
           
        }


        public void getEmail(string page)
        {
            Regex emailrx = new Regex(emailRE);
            MatchCollection matches = emailrx.Matches(page);
            foreach (Match match in matches)
            {
                emailTexBox.Text = emailTexBox.Text + match.Value + "\r\n";
                
            }
            
        }
        //一下两个方法的输出写同一个文本框
        public void getPhone(string page)
        {
            Regex phonerx = new Regex(phoneRE);
            MatchCollection matches = phonerx.Matches(page);
            foreach (Match match in matches)
            {
                phoneTexBox.Text = phoneTexBox.Text + match.Value + "\r\n";
            }
        }
        public void getTelPhone(string page)
        {
            Regex telphonerx = new Regex(telphoneRE);
            MatchCollection matches = telphonerx.Matches(page);
            foreach (Match match in matches)
            {
                
                phoneTexBox.Text = phoneTexBox.Text + match.Value + "\r\n";
            }
        }
        //一次检索全部，包括邮箱，手机
        private void buttonStart_Click(object sender, EventArgs e)
        {
            string url = urlTexBox.Text;
            string webPage = getHtmlString(url);
            getEmail(webPage);
            getPhone(webPage);
            getTelPhone(webPage);
        }
        //只检索手机
        private void searchPhone_Click(object sender, EventArgs e)
        {
            string url = urlTexBox.Text;
            string webPage = getHtmlString(url);
            getPhone(webPage);
            getTelPhone(webPage);
        }
        //只检索邮箱
        private void searchEmail_Click(object sender, EventArgs e)
        {
            string url = urlTexBox.Text;
            string webPage = getHtmlString(url);
            getEmail(webPage);
            
        }
    }
}
